package org.dromara.pe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.Data;
import org.dromara.basic.domain.Member;
import org.dromara.basic.domain.vo.MemberVo;
import org.dromara.common.core.constant.Constants;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.pe.domain.vo.TaskArrangeInfoVo;
import org.springframework.stereotype.Service;
import org.dromara.pe.domain.bo.TaskInfoBo;
import org.dromara.pe.domain.vo.TaskInfoVo;
import org.dromara.pe.domain.TaskInfo;
import org.dromara.pe.mapper.TaskInfoMapper;
import org.dromara.pe.service.ITaskInfoService;

import java.time.Duration;
import java.util.*;

/**
 * 体测任务管理Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-02
 */
@RequiredArgsConstructor
@Service
public class TaskInfoServiceImpl implements ITaskInfoService {

    private final TaskInfoMapper baseMapper;

    /**
     * 查询体测任务管理
     */
    @Override
    public TaskInfoVo queryById(Long taskId){
        return baseMapper.selectVoById(taskId);
    }

    /**
     * 查询体测任务管理列表
     */
    @Override
    public TableDataInfo<TaskInfoVo> queryPageList(TaskInfoBo bo, PageQuery pageQuery) {
        QueryWrapper<TaskInfo> lqw = buildQueryWrapper(bo);
        Page<TaskInfoVo> result = baseMapper.selectTaskInfoList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询体测任务管理列表
     */
    @Override
    public List<TaskInfoVo> queryList(TaskInfoBo bo) {
        QueryWrapper<TaskInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectTaskInfoList(lqw);
    }

    private QueryWrapper<TaskInfo> buildQueryWrapper(TaskInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<TaskInfo> lqw = new QueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), "ppti.task_name", bo.getTaskName());
        lqw.eq(bo.getTaskType() != null, "ppti.task_type", bo.getTaskType());
        lqw.eq(bo.getTaskStatus() != null, "ppti.task_status", bo.getTaskStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getSemester()), "ppti.semester", bo.getSemester());
        lqw.eq(StringUtils.isNotBlank(bo.getAcademicYear()), "ppti.academic_year", bo.getAcademicYear());

        return lqw;
    }

    /**
     * 新增体测任务管理
     */
    @Override
    public Boolean insertByBo(TaskInfoBo bo) {
        TaskInfo add = MapstructUtils.convert(bo, TaskInfo.class);
        add.setTaskStatus(0L);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
//        if (flag) {
//            Date date=new Date();
//            bo.setTaskId(add.getTaskId());
//            String key="taskInfo|"+add.getTaskId().toString();
//            long times = add.getEndDate().getTime() - date.getTime();
//            RedisUtils.setCacheObject(key, "value", Duration.ofMinutes(times/1000));
//        }
        return flag;
    }

    /**
     * 修改体测任务管理
     */
    @Override
    public Boolean updateByBo(TaskInfoBo bo) {
        TaskInfo update = MapstructUtils.convert(bo, TaskInfo.class);
//        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaskInfo entity){
        List<Long> statusList=new ArrayList<>();
        statusList.add(0L);
        statusList.add(1L);
        TaskInfoVo taskInfoVo = baseMapper.selectVoOne(
            new LambdaQueryWrapper<TaskInfo>()
                .in(TaskInfo::getTaskStatus, statusList)
                .eq(entity.getTaskType()!=null,TaskInfo::getTaskType, entity.getTaskType())
        );
        if (ObjectUtils.isNotEmpty(taskInfoVo)) {
            if("0".equals(entity.getTaskType().toString())){
                throw new ServiceException("当前有主任务未完成，请完成后再新建任务");
            }else if("1".equals(entity.getTaskType().toString())){
                throw new ServiceException("当前有临时任务未完成，请完成后再新建任务");
            }else {
                throw new ServiceException("当前有任务未完成，请完成后再新建任务");
            }
        }
    }

    /**
     * 批量删除体测任务管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


}
